package com.jxb.netty.network.bio;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerSingle {

    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket();
        serverSocket.bind(new InetSocketAddress(777));
        System.out.println("start server ... ...");
        int connectCount = 0;
        try {
            while (true) {
                Socket socket = serverSocket.accept();
                System.out.println("accept client socket ....total =" + ( ++connectCount));
                try (ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
                     ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream())){
                    //服务端的输入
                    String msg = objectInputStream.readUTF();
                    System.out.println("Accept client message:"+msg);
                    //客户端的输入
//                    objectOutputStream.writeUTF("Hello,"+msg);
//                    objectOutputStream.flush();
                }catch (Exception e) {
                    e.printStackTrace();
                }finally {
                    socket.close();
                }
            }
        }finally {
            serverSocket.close();
        }
    }

}
